package com.jc.order.service.impl;

import com.jc.order.domain.KjbInsuranceRateSetting;
import com.jc.order.dto.KjbInsuranceRateSettingDTO;
import com.jc.order.mapper.KjbInsuranceRateSettingMapper;
import com.jc.order.service.KjbInsuranceRateSettingService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;


@Service
public class KjbInsuranceRateSettingServiceImpl implements KjbInsuranceRateSettingService {

    @Resource
    private KjbInsuranceRateSettingMapper kjbInsuranceRateSettingMapper;

    @Override
    public KjbInsuranceRateSettingDTO selectInsuranceRateSetting(KjbInsuranceRateSettingDTO dto) {

        KjbInsuranceRateSettingDTO rateSettingDTO = null;
        //如果 客户  fee_version_type不等于9的情况下   查询费率 ten_id要传0,
        Long tenId =dto.getTenId();
        if (9!=dto.getVersionType()){
            tenId =0L;
        }
        Example example = new Example(KjbInsuranceRateSetting.class, true, true);
        Example.Criteria criteria = example.createCriteria()
                .andEqualTo("tenId", tenId)
                .andEqualTo("insuranceType", dto.getInsuranceType())
                .andEqualTo("versionType", dto.getVersionType())
                .andEqualTo("deleted",false);
        if ("1".equalsIgnoreCase(dto.getInsuranceType().toString())) {
//            criteria.andEqualTo("transportType", dto.getTransportType())
//                    .andEqualTo("dispatchType", dto.getDispatchType())
//                    .andEqualTo("shelfGuarantee", dto.getShelfGuarantee());


            //干线运输方式 != 公路（4）或 全程快递（5） 就是海运（1）
            Integer transportType = dto.getTransportType().intValue() == 4 || dto.getTransportType().intValue() == 5 ? dto.getTransportType().intValue() : 1;
            //派送方式 = 待定（3） 就是 快递派（1），否则判断运输方式== 全程快递（5） 就是 0
            Integer dispatchType = dto.getDispatchType().intValue() == 3 ? 1 : dto.getTransportType().intValue() == 5 ? 0 : dto.getDispatchType();
            criteria.andEqualTo("transportType", transportType);
            criteria.andEqualTo("dispatchType", dispatchType);
            criteria.andEqualTo("shelfGuarantee", dto.getShelfGuarantee());


        }
        List<KjbInsuranceRateSetting> list = kjbInsuranceRateSettingMapper.selectByExample(example);
        // 返回一条 数据才是正确的；
        if (list != null && list.size() == 1) {
            rateSettingDTO = new KjbInsuranceRateSettingDTO();
            BeanUtils.copyProperties(list.get(0), rateSettingDTO);
        }
        return rateSettingDTO;
    }
}
